package com.example.myapplication.screen

import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Button
import androidx.compose.material3.Checkbox
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp


@Composable
fun HobbySelection(innerPadding: PaddingValues) {
    val hobbies = listOf("Reading", "Traveling", "Cooking") // 爱好列表
    val selectedHobbies = remember { mutableStateOf(mutableListOf<String>()) } // 存储选中爱好的集合

    Column(
        modifier = Modifier.padding(16.dp).padding(innerPadding)
    ) {
        Text(text = "请选择你的爱好:")

        // 为每个爱好创建一个复选框
        hobbies.forEach { hobby ->
            Row(verticalAlignment = Alignment.CenterVertically) {
                Checkbox(
                    checked = selectedHobbies.value.contains(hobby),
                    onCheckedChange = { isChecked ->
                        //根据选中状态操作集合中的元素，如果选中则添加到selectedHobbies集合，否则从selectedHobbies集合中删除
                        selectedHobbies.value = selectedHobbies.value.toMutableList().apply {
                            if (isChecked) add(hobby) else remove(hobby)
                        }
                    }
                )
                Text(
                    text = hobby,
                    modifier = Modifier
                        .clickable {
                            // 点击文本也可以切换复选框状态
                            selectedHobbies.value = selectedHobbies.value.toMutableList().apply {
                                if (contains(hobby)) remove(hobby) else add(hobby)
                            }
                        }
                        .padding(start = 8.dp) // 文字与复选框间距
                )
            }
        }

        // 提交按钮，点击时输出所有选中的爱好
        Button(onClick = {
            println("已经选中的爱好：${selectedHobbies.value.toString()}")
        }) {
            Text("点击提交")
        }
    }
}